package com.example.vaccine.controller;

import com.example.vaccine.VO.PageVO;
import com.example.vaccine.VO.ResultVO;
import com.example.vaccine.form.AssignCheckTaskForm;
import com.example.vaccine.service.CheckTasksService;
import com.example.vaccine.util.ResultVOUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>前端控制器</p>
 */
@RestController
@RequestMapping("/checkTasks")
@Tag(name = "核查任务管理", description = "CheckTasksController")
public class CheckTasksController {

    @Autowired
    private CheckTasksService checkTasksService;

    /**
     * 给核查员分配疫苗核查任务
     */
    @Operation(summary = "分配核查任务", description = "给指定核查员分配多个疫苗核查任务")
    @PostMapping("/assign")
    public ResultVO assign(@RequestBody AssignCheckTaskForm assignForm) {
        Boolean assigned = checkTasksService.assignVaccinesToChecker(assignForm.getCheckerId(), assignForm.getVaccines());
        if (!assigned) {
            return ResultVOUtil.fail();
        }
        return ResultVOUtil.success(null);
    }

    /**
     * 获取未分配疫苗分页数据
     */
    @Operation(summary = "获取未分配疫苗", description = "分页获取尚未分配核查任务的疫苗")
    @GetMapping("/unassignedVaccines/{page}/{size}")
    public ResultVO getUnassignedVaccines(
            @Parameter(description = "页码") @PathVariable Integer page,
            @Parameter(description = "每页条数") @PathVariable Integer size) {
        PageVO pageVO = checkTasksService.getUnassignedVaccines(page, size);
        return ResultVOUtil.success(pageVO);
    }

    /**
     * 获取核查任务分页列表
     */
    @Operation(summary = "分页获取任务列表", description = "获取所有核查任务的分页数据")
    @GetMapping("/list")
    public ResultVO getTasks(
            @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") int page,
            @Parameter(description = "每页条数", example = "5") @RequestParam(defaultValue = "5") int pageSize
    ) {
        return checkTasksService.getPaginatedTasks(page, pageSize);
    }

    /**
     * 获取核查任务的状态统计
     */
    @Operation(summary = "获取状态统计", description = "获取核查任务按状态分类的数量汇总")
    @GetMapping("/statusSummary")
    public ResultVO getStatusSummary() {
        Map<String, Integer> statusMap = checkTasksService.getStatusCounts();
        return ResultVOUtil.success(statusMap);
    }
}
